package cn.tedu.charging.order.config;

import cn.tedu.common.pojo.constants.RabbitConstants;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class RabbitMQConfiguration {

    public static final String EXCHANGE_NAME = "charging_order_exchange";
    public static final String QUEUE_NAME = "charging_order_queue";
    public static final String ROUTING_KEY = "charging_order_routing_key";

    public static final String DEVICE_EXCHANGE_NAME = "device_charging_order_exchange";
    public static final String DEVICE_ROUTING_KEY = "device_charging_order_routing_key";

    public static final String DEAD_LETTER_EXCHANGE = "dead_letter_charging_order_exchange";
    public static final String DEAD_LETTER_QUEUE = "dead_letter_charging_order_queue";
    public static final String DEAD_LETTER_ROUTING_KEY = "dead_letter_charging_order_routing_key";

    /**
     * 通过@Bean 定义exchange
     * @return
     */
    @Bean
    public DirectExchange orderExchange() {
        //消息持久化 不放到内存里，放到磁盘，重启RabbitMQ 消息还在不丢失
        Boolean durable = true;
        //自动删除
        Boolean autoDelete = false;

        return new DirectExchange(EXCHANGE_NAME, durable, autoDelete);
    }

    /**
     * 通过@Bean 定义 Queue
     * 同时设置消息的 生存时间
     * 同时设置死信队列和路由key
     * @return
     */
    @Bean
    public Queue orderQueue(){
        //消息持久化 不放到内存里，放到磁盘，重启RabbitMQ 消息还在不丢失
        Boolean durable = true;
        //排他队列
        Boolean exclusive = false;
        //自动删除
        Boolean autoDelete = false;

        Map<String, Object> arguments = new HashMap<>();
        //惰性队列,消息很多的时候把消息存储到磁盘，避免消息挤压，占用内存
        arguments.put("x-queue-mode", "lazy");

        //ttl TIME-TO-LIVE 设置消息的生存时间
        Integer ttl = 2 * 60 * 1000; //2分钟
        arguments.put("x-message-ttl", ttl);
        //设置死信Exchange
        arguments.put("x-dead-letter-exchange",DEAD_LETTER_EXCHANGE);
        //设置死信RoutingKey
        arguments.put("x-dead-letter-routing-key",DEAD_LETTER_ROUTING_KEY);

        return new Queue(QUEUE_NAME, durable,exclusive, autoDelete, arguments);
    }

    /**
     * 把Queue和Exchange 通过RoutingKey进行绑定
     * @return
     */
    @Bean
    public Binding orderBinding(){
        return BindingBuilder.bind(orderQueue()).to(orderExchange()).with(ROUTING_KEY);
    }


    /**
     * 通过@Bean 定义 死信 exchange
     * @return
     */
    @Bean
    public DirectExchange deadLetterExchange(){
        //消息持久化 不放到内存里，放到磁盘，重启RabbitMQ 消息还在不丢失
        Boolean durable = true;
        //自动删除
        Boolean autoDelete = false;
        return new DirectExchange(DEAD_LETTER_EXCHANGE, durable, autoDelete);
    }

    /**
     * 通过@Bean 定义 死信 queue
     * @return
     */
    @Bean
    public Queue deadLetterQueue(){
        //消息持久化 不放到内存里，放到磁盘，重启RabbitMQ 消息还在不丢失
        Boolean durable = true;
        //自动删除
        Boolean autoDelete = false;
        return new Queue(DEAD_LETTER_QUEUE, durable, false, autoDelete);
    }

    /**
     * 把死信Queue和死信Exchange 通过RoutingKey进行绑定
     * @return
     */
    @Bean
    public Binding deadLetterBinding(){
        return BindingBuilder.bind(deadLetterQueue())
                .to(deadLetterExchange()).with(DEAD_LETTER_ROUTING_KEY);
    }


    /**
     * 通过@Bean 定义 死信 exchange
     * @return
     */
    @Bean
    public DirectExchange deviceExchange(){
        //消息持久化 不放到内存里，放到磁盘，重启RabbitMQ 消息还在不丢失
        Boolean durable = true;
        //自动删除
        Boolean autoDelete = false;
        return new DirectExchange(DEVICE_EXCHANGE_NAME, durable, autoDelete);
    }

    /**
     * 通过@Bean 定义 死信 queue
     * @return
     */
    @Bean
    public Queue deviceQueue(){
        //消息持久化 不放到内存里，放到磁盘，重启RabbitMQ 消息还在不丢失
        Boolean durable = true;
        //自动删除
        Boolean autoDelete = false;
        return new Queue(RabbitConstants.DEVICE_QUEUE_NAME,durable, false, autoDelete);
    }

    /**
     * 把死信Queue和死信Exchange 通过RoutingKey进行绑定
     * @return
     */
    @Bean
    public Binding deviceBinding(){
        return BindingBuilder.bind(deviceQueue())
                .to(deviceExchange()).with(DEVICE_ROUTING_KEY);
    }

}
